
package com.eltsistemas.view.funcionario;

import com.eltsistemas.adapter.Mensagem;
import com.eltsistemas.adapter.Tabela;
import com.eltsistemas.controller.FuncionarioController;
import com.eltsistemas.modelo.Funcionario;
import java.util.List;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Frm_Funcionario extends javax.swing.JDialog {

    private static Frm_FuncionarioAdd frm_FuncionarioAdd;
    private static Frm_FuncionarioAlt frm_FuncionarioAlt;

    public Frm_Funcionario() {
        initComponents();
        setLocationRelativeTo(null);
        tamanhoTabela();
        carregaTabela();
    }
    
    private void tamanhoTabela()
    {
        DefaultTableCellRenderer centro = new DefaultTableCellRenderer();  
        centro.setHorizontalAlignment(SwingConstants.CENTER);  

        tabela.getColumnModel().getColumn( 0 ).setCellRenderer(centro);
        tabela.getColumnModel().getColumn( 1 ).setCellRenderer(centro);
        tabela.getColumnModel().getColumn( 3 ).setCellRenderer(centro);
        tabela.getColumnModel().getColumn( 4 ).setCellRenderer(centro);
        tabela.getColumnModel().getColumn( 6 ).setCellRenderer(centro);
        tabela.getColumnModel().getColumn( 7 ).setCellRenderer(centro);
        
        tabela.getColumnModel().getColumn( 0 ).setPreferredWidth( 20 );
        tabela.getColumnModel().getColumn( 1 ).setPreferredWidth( 20 );
        tabela.getColumnModel().getColumn( 2 ).setPreferredWidth( 220 ); 
        tabela.getColumnModel().getColumn( 3 ).setPreferredWidth( 80 ); 
        tabela.getColumnModel().getColumn( 4 ).setPreferredWidth( 100 ); 
        tabela.getColumnModel().getColumn( 5 ).setPreferredWidth( 100 ); 
        tabela.getColumnModel().getColumn( 6 ).setPreferredWidth( 50 );
        tabela.getColumnModel().getColumn( 7 ).setPreferredWidth( 50 ); 

    }
    
    class Progresso extends Thread
    {
        @Override
        public void run(){  
        int a = 0, b = 1;
        DefaultTableModel tableModel =(DefaultTableModel) tabela.getModel();
        jProgressBar.setIndeterminate(true);
        jProgressBar.setVisible(true);
       
        //componentDisable();
            
        FuncionarioController funcionarioController = new FuncionarioController();
          
        List<Funcionario> funcionarioList = funcionarioController.lista();
            
        for(Funcionario f : funcionarioList)
        {
            tableModel.setNumRows( b ) ;
            tabela.setValueAt(b, a, 0);
            tabela.setValueAt( f.getId(), a, 1 );
            tabela.setValueAt( f.getNomeCompleto(), a, 2 );
            tabela.setValueAt(f.getCpf(), a, 3);
            tabela.setValueAt(f.getRg(), a, 4);
            tabela.setValueAt(f.getCelular(), a, 5);
            tabela.setValueAt(f.getComissao(), a, 6);
            tabela.setValueAt(f.getFuncao(), a, 7);
           
            a++;
            b++;
        }
       
        labelLinha.setText("  Total de linhas: "+a+" ");

        tabela.getSelectionModel().clearSelection();
        tabela.repaint();
        
        jProgressBar.setIndeterminate(false);
        jProgressBar.setVisible(false);
     };
    }
    
    protected void carregaTabela()
    {
        Progresso progresso = new Progresso();
        progresso.start();
    }
    
    protected void dialog_FuncionarioAdd()
    {
        frm_FuncionarioAdd = new Frm_FuncionarioAdd();
        frm_FuncionarioAdd.setObject(this);
        frm_FuncionarioAdd.setModal(true);
        frm_FuncionarioAdd.setVisible(true);
    }
    
    private void exibeDados()
    {
        if(tabela.getSelectedRow() >= 0)
        {
            int linha = tabela.getSelectedRow();
            Long codigo = (Long) tabela.getValueAt(linha, 1);
            
            Funcionario funcionario = new Funcionario();
            funcionario.setId( codigo );
            
            FuncionarioController funcionarioController = new FuncionarioController();
           
            frm_FuncionarioAlt = new Frm_FuncionarioAlt();
            frm_FuncionarioAlt.exibeDados( funcionarioController.exibeDados(funcionario) );
            frm_FuncionarioAlt.setObject(this);
            frm_FuncionarioAlt.setModal(true);
            frm_FuncionarioAlt.setVisible(true);
        }
        
        else
        {
            new Mensagem().msg_LinhaTabela();
        }
    }
    
    private void delete()
    { 
        if(tabela.getSelectedRow() >= 0)
        {
            int linha = tabela.getSelectedRow();
            Long codigo = (Long) tabela.getValueAt(linha, 1);
            
            Funcionario funcionario = new Funcionario();
            funcionario.setId( codigo );
            
            FuncionarioController funcionarioController = new FuncionarioController();
            funcionarioController.delete(funcionario);
            
            new Mensagem().msg_Exito("Funcionário", "excluído");
            carregaTabela();
        }
        
        else
        {
            new Mensagem().msg_LinhaTabela();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jToolBar = new javax.swing.JToolBar();
        btn_novo = new javax.swing.JButton();
        btn_editar = new javax.swing.JButton();
        btn_excluir = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btn_atualizar = new javax.swing.JButton();
        btn_primeiro = new javax.swing.JButton();
        btn_anterior = new javax.swing.JButton();
        btn_superior = new javax.swing.JButton();
        btn_ultimo = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        labelLinha = new javax.swing.JLabel();
        jProgressBar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Funcionários | Cadastrados");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tabela.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "Cód.", "Nome completo", "CPF", "RG", "Celular", "Comissao(%)", "Função"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setFillsViewportHeight(true);
        tabela.setSelectionBackground(java.awt.Color.yellow);
        tabela.setSelectionForeground(java.awt.Color.black);
        tabela.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabela.getTableHeader().setReorderingAllowed(false);
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tabelaMousePressed(evt);
            }
        });
        jScrollPane.setViewportView(tabela);

        jToolBar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar.setFloatable(false);
        jToolBar.setRollover(true);

        btn_novo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/eltsistemas/img/table_add.png"))); // NOI18N
        btn_novo.setFocusable(false);
        btn_novo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_novo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_novoActionPerformed(evt);
            }
        });
        jToolBar.add(btn_novo);

        btn_editar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/eltsistemas/img/table_edit.png"))); // NOI18N
        btn_editar.setFocusable(false);
        btn_editar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_editar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_editarActionPerformed(evt);
            }
        });
        jToolBar.add(btn_editar);

        btn_excluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/eltsistemas/img/table_delete.png"))); // NOI18N
        btn_excluir.setFocusable(false);
        btn_excluir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_excluir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_excluirActionPerformed(evt);
            }
        });
        jToolBar.add(btn_excluir);
        jToolBar.add(jSeparator1);

        btn_atualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/eltsistemas/img/refresh.png"))); // NOI18N
        btn_atualizar.setFocusable(false);
        btn_atualizar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_atualizar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_atualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_atualizarActionPerformed(evt);
            }
        });
        jToolBar.add(btn_atualizar);

        btn_primeiro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/eltsistemas/img/seta_first.png"))); // NOI18N
        btn_primeiro.setFocusable(false);
        btn_primeiro.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_primeiro.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_primeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_primeiroActionPerformed(evt);
            }
        });
        jToolBar.add(btn_primeiro);

        btn_anterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/eltsistemas/img/seta_previous.png"))); // NOI18N
        btn_anterior.setFocusable(false);
        btn_anterior.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_anterior.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_anterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_anteriorActionPerformed(evt);
            }
        });
        jToolBar.add(btn_anterior);

        btn_superior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/eltsistemas/img/seta_next.png"))); // NOI18N
        btn_superior.setFocusable(false);
        btn_superior.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_superior.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_superior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_superiorActionPerformed(evt);
            }
        });
        jToolBar.add(btn_superior);

        btn_ultimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/eltsistemas/img/seta_last.png"))); // NOI18N
        btn_ultimo.setFocusable(false);
        btn_ultimo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_ultimo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_ultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ultimoActionPerformed(evt);
            }
        });
        jToolBar.add(btn_ultimo);
        jToolBar.add(jSeparator2);

        labelLinha.setText("   Total de linhas: ");
        jToolBar.add(labelLinha);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 824, Short.MAX_VALUE)
            .addComponent(jToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tabelaMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMousePressed

    }//GEN-LAST:event_tabelaMousePressed

    private void btn_novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_novoActionPerformed
        dialog_FuncionarioAdd();
    }//GEN-LAST:event_btn_novoActionPerformed

    private void btn_editarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_editarActionPerformed
        exibeDados();
    }//GEN-LAST:event_btn_editarActionPerformed

    private void btn_excluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_excluirActionPerformed
        delete();
    }//GEN-LAST:event_btn_excluirActionPerformed

    private void btn_atualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_atualizarActionPerformed
        carregaTabela();
    }//GEN-LAST:event_btn_atualizarActionPerformed

    private void btn_primeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_primeiroActionPerformed
        new Tabela().firstLine(tabela,jScrollPane);
    }//GEN-LAST:event_btn_primeiroActionPerformed

    private void btn_anteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_anteriorActionPerformed
        new Tabela().previousLine(tabela,jScrollPane);
    }//GEN-LAST:event_btn_anteriorActionPerformed

    private void btn_superiorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_superiorActionPerformed
        new Tabela().nextLine(tabela,jScrollPane);
    }//GEN-LAST:event_btn_superiorActionPerformed

    private void btn_ultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ultimoActionPerformed
        new Tabela().lastLine(tabela,jScrollPane);
    }//GEN-LAST:event_btn_ultimoActionPerformed
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Frm_Funcionario dialog = new Frm_Funcionario();
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_anterior;
    private javax.swing.JButton btn_atualizar;
    private javax.swing.JButton btn_editar;
    private javax.swing.JButton btn_excluir;
    private javax.swing.JButton btn_novo;
    private javax.swing.JButton btn_primeiro;
    private javax.swing.JButton btn_superior;
    private javax.swing.JButton btn_ultimo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar jProgressBar;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar;
    private javax.swing.JLabel labelLinha;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables
}
